package com.leonzx.base.service.im;

import com.leonzx.base.constants.IMConstants;
import com.leonzx.base.dao.system.RoleDAO;
import com.leonzx.base.dao.system.UserDAO;
import com.leonzx.base.entity.im.ImFriendGroupUserData;
import com.leonzx.base.entity.im.ImFriendUserInfoData;
import com.leonzx.base.entity.im.ImGroupUserData;
import com.leonzx.base.entity.system.Orders;
import com.leonzx.base.entity.system.Role;
import com.leonzx.base.entity.system.User;
import com.leonzx.base.service.BaseService;
import com.leonzx.base.service.RedisService;
import com.leonzx.base.service.system.OrdersService;
import com.leonzx.base.utils.Constants;
import com.leonzx.base.utils.RedisConstants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author:李晓雷
 * @Description:
 * @Date: 18/07/13 14:03
 */
@Service
public class ImService extends BaseService {
    @Autowired
    private RoleDAO roleDAO;
    @Autowired
    private UserDAO userDAO;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private RedisService redisService;

    /**
     * 查询好友分组和下面的人员信息
     *
     * @return
     */
    public List<ImFriendGroupUserData> queryGroupAndUser(String userId) {
        //传值对象
        List<ImFriendGroupUserData> imFriedUserDataList = new ArrayList<>();
        //查询所有角色
        List<Role> roleList = roleDAO.selectAll();
        for (Role role : roleList) {
            //1分组(使用角色分组)
            ImFriendGroupUserData imFriendGroup = new ImFriendGroupUserData();
            imFriendGroup.setId(role.getId());
            imFriendGroup.setGroupname(role.getName());
            //2人员
            List<User> userList = userDAO.findUserByRoleIdAndState(role.getId(), Constants.STATE_VAILD);
            //传值对象
            List<ImFriendUserInfoData> list = new ArrayList<>();

            for (User user : userList) {
                if (Objects.equals(user.getId(), userId)) {
                    continue;
                }
                ImFriendUserInfoData imFriedUserInFoData = new ImFriendUserInfoData();
                imFriedUserInFoData.setId(user.getId());
                imFriedUserInFoData.setUsername(user.getName());
                imFriedUserInFoData.setAvatar("/static/im/images/user.png");//默认头像
                imFriedUserInFoData.setSign(user.getImSign());//签名

                String online = redisService.getValue(RedisConstants.JIM_USER_STATUS + user.getId());

                if (StringUtils.isNotBlank(online)) {
                    String status = user.getImStatus();
                    status = Objects.equals(status, IMConstants.IM_STATUS_HIDE) ? IMConstants.IM_STATUS_OFFLINE : status;
                    imFriedUserInFoData.setStatus(status);
                } else {
                    imFriedUserInFoData.setStatus(IMConstants.IM_STATUS_OFFLINE);
                }
                list.add(imFriedUserInFoData);
            }
            imFriendGroup.setList(list);
            imFriedUserDataList.add(imFriendGroup);
        }
        return imFriedUserDataList;
    }

    /**
     * 业务是默认人员进入所有委托state=1的群组
     *
     * @return
     */
    public List<ImGroupUserData> getGroups() {
        List<ImGroupUserData> groupList = new ArrayList<>();
        Orders orders = new Orders();
        orders.setState(Constants.STATE_VAILD);
        List<Orders> ordersList = ordersService.getAllBaseOrders(orders);
        for (Orders ino : ordersList) {
            ImGroupUserData group = new ImGroupUserData();
            //默认群组图标
            group.setAvatar("/static/im/images/usergroup.png");
            group.setId(ino.getId());
            group.setGroupname(ino.getNumber());
            groupList.add(group);
        }
        return groupList;
    }

    /**
     * 手机登陆获取LayIm初始数据
     *
     * @param user
     * @return
     */
    public Map<String, Object> getLayImInitData(User user) {
        ImFriendUserInfoData my = new ImFriendUserInfoData();
        //1设置自己信息
        my.setId(user.getId());
        my.setAvatar("/static/im/images/user.png");
        my.setSign(user.getImSign());
        my.setUsername(user.getName());
        my.setStatus(user.getImStatus());
        Map map = new HashMap();
        map.put("mine", my);
        //2设置好友信息
        List<ImFriendGroupUserData> friends = queryGroupAndUser(user.getId());
        map.put("friend", friends);
        //设置群组信息
        List<ImGroupUserData> groups = getGroups();
        map.put("group", groups);
        return map;
    }
}
